/*

    This file is part of GLua

    GLua Development Environment
    Copyright (C) 2007 VoiDeD

    GLua is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    GLua is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Net;
using System.IO;

namespace GLua
{
    /// <summary>
    /// Legacy class, was used during beta testing
    /// </summary>
    public class IPChecker
    {
        public static BetaInformation CheckIP(string ip)
        {
            HttpWebRequest request = ( HttpWebRequest )WebRequest.Create( StringTable.IPCheckURL );
            request.UserAgent = StringTable.UserAgent;
            HttpWebResponse response = null;
            StreamReader reader = null;
            try
            {
                response = ( HttpWebResponse )request.GetResponse();
                reader = new StreamReader( response.GetResponseStream() );
            }
            catch
            {
                BetaInformation bi = new BetaInformation();
                bi.CanUse = false;
                bi.Error = true;

                bi.ErrorString = StringTable.CouldNotContactHost;

                return bi;
            }

            string str = reader.ReadLine();

            if ( str.StartsWith( "Error" ) )
            {
                BetaInformation bi = new BetaInformation();
                bi.CanUse = false;
                bi.Error = true;

                bi.ErrorString = StringTable.ValidationError;

                return bi;
            }
            else
            {
                if ( str == ip )
                {
                    BetaInformation bi = new BetaInformation();
                    bi.CanUse = true;
                    bi.Error = false;

                    bi.ErrorString = "";

                    return bi;
                }
            }

            BetaInformation binfo = new BetaInformation();
            binfo.CanUse = false;
            binfo.Error = true;

            binfo.ErrorString = StringTable.UnableToValidate;

            return binfo;
        }
        public static BetaInformation CheckIP( string[] ips )
        {
            HttpWebRequest request = ( HttpWebRequest )WebRequest.Create( StringTable.IPCheckURL );
            request.UserAgent = StringTable.UserAgent;
            HttpWebResponse response = null;
            StreamReader reader = null;
            try
            {
                response = ( HttpWebResponse )request.GetResponse();
                reader = new StreamReader( response.GetResponseStream() );
            }
            catch
            {
                BetaInformation bi = new BetaInformation();
                bi.CanUse = false;
                bi.Error = true;

                bi.ErrorString = StringTable.CouldNotContactHost;

                return bi;
            }

            string str = reader.ReadLine();

            if ( str.StartsWith( "Error" ) )
            {
                BetaInformation bi = new BetaInformation();
                bi.CanUse = false;
                bi.Error = true;

                bi.ErrorString = StringTable.ValidationError;

                return bi;
            }
            else
            {
                foreach ( string ip in ips )
                {
                    if ( str == ip )
                    {
                        BetaInformation bi = new BetaInformation();
                        bi.CanUse = true;
                        bi.Error = false;

                        bi.ErrorString = "";

                        return bi;
                    }
                    else
                    {
                        continue;
                    }
                }
            }

            BetaInformation binfo = new BetaInformation();
            binfo.CanUse = false;
            binfo.Error = true;

            binfo.ErrorString = StringTable.UnableToValidate;

            return binfo;
        }
    }
}
